package com.itheima.service.impl;

import com.itheima.dao.GoodsMapper;
import com.itheima.goods.pojo.Goods;
import com.itheima.service.SearchService1;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class SearchService1Impl implements SearchService1 {
    @Autowired
    private  GoodsMapper goodsMapper;
    private RestHighLevelClient client;
    // 索引库名称
    private final static String INDEX_NAME = "goodsinfo";
    @Override
    public void importAll() {
        //获取商品的所有数据
        List<Goods> goods = goodsMapper.selectList(null);
        //判断索引库是否存在
        if (isExist()){
            //存在的话就删除索引库
            deleteIndex();
        }
        //不存在就创建
        createIndex();

    }

    private boolean isExist(){
        GetIndexRequest request = new GetIndexRequest(INDEX_NAME);
        try {
            return client.indices().exists(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private void deleteIndex(){
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(INDEX_NAME);
        try {
            client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void createIndex(){
        CreateIndexRequest request = new CreateIndexRequest(INDEX_NAME);
        try {
            request.mapping("{" +
                    "\"properties\": {" +
                    "\"id\": {" +
                    "\"type\": \"integer\"" +
                    "}," +
                    "\"image\": {" +
                    "\"index\": false," +
                    "\"type\": \"keyword\"" +
                    "}," +
                    "\"brandId\": {" +
                    "\"type\": \"integer\"" +
                    "}," +
                    "\"categoryId\": {" +
                    "\"type\": \"integer\"" +
                    "}," +
                    "\"brandName\": {" +
                    "\"type\": \"keyword\"" +
                    "}," +
                    "\"categoryName\": {" +
                    "\"type\": \"keyword\"" +
                    "}," +
                    "\"num\": {" +
                    "\"type\": \"integer\"" +
                    "}," +
                    "\"specMap\": {" +
                    "\"type\": \"object\"" +
                    "}," +
                    "\"updateTime\": {" +
                    "\"type\": \"date\"" +
                    "}," +
                    "\"spec\": {" +
                    "\"type\": \"keyword\"" +
                    "}," +
                    "\"createTime\": {" +
                    "\"type\": \"date\"" +
                    "}," +
                    "\"price\": {" +
                    "\"type\": \"integer\"" +
                    "}," +
                    "\"name\": {" +
                    "\"analyzer\": \"ik_max_word\"," +
                    "\"type\": \"text\"" +
                    "}," +
                    "\"status\": {" +
                    "\"type\": \"keyword\"" +
                    "}" +
                    "}" +
                    "}", XContentType.JSON);
            client.indices().create(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
